<template>
  <div class="family-container">
    <div class="header">
      <h2>家庭管理</h2>
      <el-button type="primary" @click="handleAdd">新增家庭</el-button>
    </div>

    <!-- 表格 -->
    <el-table :data="familyList" style="width: 100%" v-loading="loading" border stripe>
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="family_name" label="家庭名称" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="admin_user_id" label="管理员ID" width="100" />
      <el-table-column prop="created_at" label="创建时间" width="180" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogType === 'add' ? '新增家庭' : '编辑家庭'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="家庭名称" prop="family_name">
          <el-input v-model="formData.family_name" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="管理员ID" prop="admin_user_id">
          <el-input-number v-model="formData.admin_user_id" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getFamilyList,
  addFamily,
  updateFamily,
  deleteFamily
} from '@/api/family'

// 数据列表
const familyList = ref([])
const loading = ref(false)

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const formRef = ref(null)
const formData = ref({
  family_name: '',
  address: '',
  admin_user_id: ''
})

// 表单校验规则
const rules = {
  family_name: [{ required: true, message: '请输入家庭名称', trigger: 'blur' }],
  address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
  admin_user_id: [{ required: true, message: '请输入管理员ID', trigger: 'blur' }]
}

// 获取列表数据
const getList = async () => {
  try {
    loading.value = true
    const res = await getFamilyList()
    familyList.value = res.data
  } catch (error) {
    console.error('获取家庭列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 新增按钮点击
const handleAdd = () => {
  dialogType.value = 'add'
  formData.value = {
    family_name: '',
    address: '',
    admin_user_id: ''
  }
  dialogVisible.value = true
}

// 编辑按钮点击
const handleEdit = (row) => {
  dialogType.value = 'edit'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除按钮点击
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该家庭吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteFamily(row.family_id)
      ElMessage.success('删除成功')
      getList()
    } catch (error) {
      console.error('删除失败:', error)
    }
  })
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (dialogType.value === 'add') {
          await addFamily(formData.value)
          ElMessage.success('添加成功')
        } else {
          await updateFamily(formData.value)
          ElMessage.success('修改成功')
        }
        dialogVisible.value = false
        getList()
      } catch (error) {
        console.error('操作失败:', error)
      }
    }
  })
}

// 组件挂载时获取数据
onMounted(() => {
  getList()
})
</script>

<style scoped>
.family-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 